package com.gepengjun.lims.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gepengjun.lims.entity.Permission;
import com.gepengjun.lims.entity.RefPermissionRole;
import com.gepengjun.lims.mapper.PermissionMapper;
import com.gepengjun.lims.mapper.RefPermissionRoleMapper;
import com.gepengjun.lims.service.RefPermissionRoleService;
import com.gepengjun.lims.service.impl.base.BasePermissionServiceImpl;
import com.gepengjun.lims.util.common.CommonUtil;
import com.gepengjun.lims.util.common.DataType;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jun
 * @since 2018-08-19
 */
@Service
public class PermissionServiceImpl extends BasePermissionServiceImpl {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RefPermissionRoleService refPermissionRoleService;

    @Override
    @Cacheable(value = "permission",keyGenerator = "myKeyGenerator")
    public List<Permission> findTopMenus() {
        EntityWrapper<Permission> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("parent_Id",0L);
        entityWrapper.eq("source_type","menu");
        entityWrapper.eq("available",true);
        return  permissionMapper.selectList(entityWrapper);
    }

    @Override
    @Cacheable(value = "permission",keyGenerator = "myKeyGenerator")
    public List<Permission> findLeftMenus(Long parentId) {
        List<Permission> resultList = new ArrayList<>();
        EntityWrapper<Permission> wrapper = new EntityWrapper<>();
        wrapper.eq("parent_Id",parentId);
        wrapper.eq("source_type","menu");
        wrapper.eq("available",true);
        List<Permission> permissionList = permissionMapper.selectList(wrapper);
        if (permissionList != null && permissionList.size() > 0){
            for (Permission permission:permissionList){
                EntityWrapper<Permission> wrapper1 = new EntityWrapper<>();
                wrapper1.eq("parent_Id",permission.getPermissionId());
                wrapper1.eq("source_type","menu");
                wrapper1.eq("available",true);
                List<Permission> permissionList1 = permissionMapper.selectList(wrapper1);
                permission.setChildren(permissionList1);
                resultList.add(permission);
            }
        }
        return resultList;
    }

    @Override
    public List<Permission> findAllAvaiablePermissions() {
        EntityWrapper<Permission> wrapper = new EntityWrapper<>();
        wrapper.eq("available",true);
        return baseMapper.selectList(wrapper);
    }

    @Override
    @Cacheable(value = "permission",keyGenerator = "myKeyGenerator")
    public List<String> findPermissionStrsByList(List<Long> permissionIds) {
        List<String> permissionStrList = new ArrayList<>();
        EntityWrapper<Permission> entityWrapper = new EntityWrapper<>();
        entityWrapper.in("permission_id",permissionIds);
        entityWrapper.eq("available",true);
        List<Permission> permissionList = permissionMapper.selectList(entityWrapper);
        if (permissionList != null && permissionList.size() > 0){
            for (Permission permission:permissionList){
                permissionStrList.add(permission.getPermissionStr());
            }

        }
        return  permissionStrList;
    }

    @Override
    @Cacheable(value = "permission",keyGenerator = "myKeyGenerator")
    public List<Permission> findByUserId(Long userId) {
        return permissionMapper.findByUserId(userId);
    }

    @Override
    @CacheEvict(value = "permission",allEntries = true)
    public Boolean roleAuthorize(Long roleId, String permissionIds) {
        if (roleId != null){
            EntityWrapper<RefPermissionRole> wrapper = new EntityWrapper<>();
            wrapper.eq("role_id",roleId);
            if (deleteByRoleId(roleId)){
                if (StringUtils.isNotEmpty(permissionIds)){
                    List<Long> permissionIdList = (List<Long>) CommonUtil.covertArrayToList(permissionIds.split(","), DataType.ISLONG);
                    if (permissionIdList != null && permissionIdList.size() > 0){
                        List<RefPermissionRole> refPermissionRoleList = new ArrayList<>();
                        for (Long permissionId:permissionIdList){
                            RefPermissionRole refPermissionRole = new RefPermissionRole();
                            refPermissionRole.setRoleId(roleId);
                            refPermissionRole.setPermissionId(permissionId);
                            refPermissionRoleList.add(refPermissionRole);
                        }
                        return refPermissionRoleService.insertBatch(refPermissionRoleList);
                    }
                }else {
                    return true;
                }
            }
        }
        return false;
    }

    @CacheEvict(value = "permission",allEntries = true)
    public Boolean deleteByRoleId(Long roleId){
        if (roleId != null){
            EntityWrapper<RefPermissionRole> wrapper = new EntityWrapper<>();
            wrapper.eq("role_id",roleId);
            return refPermissionRoleService.delete(wrapper);
        }
        return false;
    }
}
